package com.wenda.admin.controller;

import com.wenda.admin.entity.TbUser;
import com.wenda.admin.mapper.UserMapper;
import com.wenda.admin.vo.Resp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
    private UserMapper userMapper;


    @RequestMapping("login")
    public String loginIndex(Model model){
        return "login";
    }

    @RequestMapping("loginForm")
    public String login(String userName, String password, Model model, HttpSession session){
        if(StringUtils.isBlank(userName) || StringUtils.isBlank(password)){
            model.addAttribute("msg","用户名或者密码不能为空");
            return loginIndex(model);
        }
        TbUser admin =  userMapper.login(userName,password);
        if(admin == null){
            model.addAttribute("msg","用户名或者密码错误");
            return loginIndex(model);
        }
        session.setAttribute("userKey",admin);
        model.addAttribute("userKey",admin);
        return "/index";
    }



    @RequestMapping("logout")
    public String logout(HttpSession session){
        session.invalidate();
        return loginIndex(null);
    }
}
